/********************************************************/
/*	Author:		gong_libin			*/
/*	Date:		2017_11_01			*/
/*	File:		CmDspEng.cpp			*/
/********************************************************/

#include "CmDspEng.h"

static char* sg_pszPort;
static CmCls::CCmPool sg_CPool;

int main(int argc, char* argv[])
{
	CmNet::CCmEpoll CEpoll;
	sg_pszPort = NULL != argv[1] ? argv[1] : (char*)"8101";

	sg_CPool.CmPoolAttrInit();
	sg_CPool.CmPoolTotalSet(CM_DSP_POOL);
	sg_CPool.CmPoolRoutineSet(CmDspEngPoolCore);
	if (CM_SUCCESS == sg_CPool.CmPoolCreate()) {
		if (CM_SUCCESS == CEpoll.CmEpollTcpInit(sg_pszPort)) {
			CEpoll.CmEpollSetFunction(CmDspEngEpollCore);
			CEpoll.CmEpollTcpWait();
		}
		else {
			CM_ERROR("Failed to CmEpollTcpInit\n");
		}
	}
	else {
		CM_ERROR("Failed to CmPoolCreate\n");
	}

	return CM_SUCCESS;
}

void* CmDspEngPoolCore(void* pPara)
{
	CMDSPPARA_S* pstPara = (CMDSPPARA_S*)pPara;
	char* pszBody = (char*)pstPara->m_uszPacket;

	if (CM_SUCCESS == strncasecmp(pszBody, CM_DSP_POST, strlen(CM_DSP_POST))) {
		if (NULL != (pszBody = strcasestr(pszBody, CM_DSP_LENG))) {
			if (NULL != (pszBody = strstr(pszBody, CM_DSP_RNRN))) {
				pszBody += strlen(CM_DSP_RNRN);
				CmDspEngRealTimeBid(pstPara->m_iSocket, pszBody);
			}
			else {
				CM_PRINT("Failed to get data!\n");
			}
		}
		else {
			CM_PRINT("Failed to get data!\n");
		}
	}
	else {
		CM_PRINT("Failed to get data!\n");
	}

	CmFree((void**)&pstPara);

	return pstPara;
}

int CmDspEngEpollCore(int iSocket)
{
	CmNet::CCmTcp CTcp;
	int iPacket = CM_FAILURE;
	CMDSPPARA_S* pstPara = NULL;
	UCHAR uszPacket[CM_KBYTES8] = { 0 };

	signal(SIGPIPE, SIG_IGN);
	CTcp.CmNetSetSocket(iSocket);

	if ((iPacket = CTcp.CmNetRecv(uszPacket, CM_KBYTES8)) > 0) {
		if (NULL != (pstPara = (CMDSPPARA_S*)CmMalloc(sizeof(CMDSPPARA_S)))) {
			pstPara->m_iSocket = iSocket;
			pstPara->m_iLength = iPacket;
			memcpy(pstPara->m_uszPacket, uszPacket, iPacket);
			sg_CPool.CmPoolDistribute(pstPara);
		}
	}

	return iPacket;
}

void CmDspEngRealTimeBid(int iSocket, char* pszBody)
{
	CCmProJsn CPro;
	CmNet::CCmTcp CTcp;
	CMADXINFO_S *pstInfo = NULL;
	char szHttp[CM_KBYTES8] = {'\0'};
	char szBody[CM_KBYTES8] = {'\0'};

	CM_PRINT("Recv: %s\n", pszBody);

	if (NULL != (pstInfo = CPro.CmProtoDeserialize(pszBody))) {
		pstInfo->m_CAdvId = "adver_id";
		pstInfo->m_CDesUrl = "http://img.ivsky.com/img/tupian/pre/201711/03/hupo-011.jpg";
		pstInfo->m_CClkUrl = "http://www.baidu.com";
		pstInfo->m_CSupUrl = "http://www.supervisory_url.com";
		pstInfo->m_CExtend = "this is dsp extend data";
		pstInfo->m_CCategory = "1,69";
		//pstInfo->m_uiCode = 1001;
		//pstInfo->m_uiMethod = 2;
		pstInfo->m_CCategory = pstInfo->m_CCats;
		pstInfo->m_uiMaxPrice = atoi(sg_pszPort) * 10;
		pstInfo->m_CCrtId  = "100";
		pstInfo->m_CCrtTitle = "DSP_1";
		pstInfo->m_uiCrtType = 1;

		CMSTR_C CData = CPro.CmProtoSerialize(pstInfo);
		memcpy(szBody, CData.c_str(), CData.length());
		CM_PRINT("Send: %s\n", szBody);
	}
	else {
		CM_PRINT("Send: NULL\n");
	}

	sprintf(szHttp, CM_DSP_HTTP, strlen(szBody), szBody);

	CTcp.CmNetSetSocket(iSocket);
	CTcp.CmNetSend((UCHAR*)szHttp, strlen(szHttp));

	return;
}
